package springmvc.param.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * description: 03
 * TODO：URL路径参数
 *
 * 1. 场景：URL路径参数直接通过 / 拼接到路径中，没有使用 ?key=value 形式的param传值
 * 2. 注解中设置动态路径 @RequestMapping("{account}/{password}")
 * 3. 方法中设置形参 @PathVariable(value="account")，属性和 @RequestParam() 一致
 * 4. 只有添加 @PathVariable 才会从路径中取值，否则默认从 param中取值
 */

@Controller
@RequestMapping("path")
@ResponseBody
public class PathController {

    // 获取URL路径中直接拼接的参数，URL路径： http://localhost:8080/path/1234/xfanonymous/abcd
    // 必须使用 @PathVariable
    @RequestMapping("{id}/{account}/{pwd}")
    public String login(@PathVariable Long id,
                        @PathVariable(value = "account") String username,
                        @PathVariable("pwd") String password) {

        System.out.println("id = " + " username = " + username + ", password = " + password);
        return "id = " + id + ", username = " + username + ", password = " + password;
        // id = 1234, username = xfanonymous, password = abcd
    }

}
